const APP = 'wj-'
export function getSessionStorage(key: string) {
  let result = localStorage.getItem(APP + key) || ''
  if (result) {
    return JSON.parse(result)
  } else {
    return result
  }
}
export function setSessionStorage(key: string, value: object) {
  localStorage.setItem(APP + key, JSON.stringify(value))
}
export function setLocalStorage(key: string, value: any) {
  localStorage.setItem(APP + key, JSON.stringify(value))
}
export function renLocalStorage(key: string, value: any) {
  localStorage.removeItem(APP + key)
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function objToStr(data: any, hf = false) {
  if (hf) {
    return JSON.stringify(data, function (key, val) {
      if (typeof val === 'function') {
        return val + '';
      }
      return val;
    });
  } else {
    return JSON.stringify(data);
  }
}
/**
 * 深拷贝
 * @param data
 */
function deepCopy(data) {
  return JSON.parse(JSON.stringify(data))
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function strToObj(data: string, hf = false) {
  if (hf) {
    return JSON.parse(data, function (k, v) {
      if (v !== undefined && v != null && v.indexOf && (v.indexOf("function ") > -1 || v.indexOf("function(") > -1)) {
        return eval("(function(){return " + v + " })()")
      }
      return v;
    });
  } else {
    // @ts-ignore
    return JSON.parse(data || null);
  }
}
// eslint-disable-next-line import/no-anonymous-default-export
export default {
  getSessionStorage,
  setSessionStorage,
  setLocalStorage,
  deepCopy
}